<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.9.1"/>
<title>cocoOS: C:/Users/peter_000/Desktop/cocoOS/common/src/docTasks.h File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">cocoOS
   &#160;<span id="projectnumber">5.0.3</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.9.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_tasks_8h.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">docTasks.h File Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p><a href="doc_tasks_8h_source.html">Go to the source code of this file.</a></p>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p><b> Tasks </b> <br />
 The main building block of your application is the task. The tasks should be created in the main function of the application before starting the os. The number of created tasks must not exceed the defined value N_TASKS in os_defines.h. A task is created this way: </p><div class="fragment"><div class="line">Msg_t queue[Q_SIZE];</div>
<div class="line"><span class="keyword">struct </span>{</div>
<div class="line"> ...</div>
<div class="line"> ...</div>
<div class="line"> ...</div>
<div class="line"> } taskData;</div>
<div class="line"></div>
<div class="line"></div>
<div class="line">taskId = <a class="code" href="os__task_8c.html#af531a501491c2acab61a65c8741e1375">task_create</a>( taskProc, &amp;taskData, prio, queue, Q_SIZE, msgSize );</div>
</div><!-- fragment --><p> The return value, <em>taskId</em>, above is just an uint8_t that id used to identify the task.<br />
 <em>taskProc</em> is a function pointer to the task procedure that is executed when the task is scheduled to run.<br />
 <em>&amp;taskData</em> is a pointer to a user defined data structure holding task data.<br />
 <em>prio</em> is the task priority in range 1-254, where 1 is highest priority. The kernel always lets the task with highest priority execute first. There can not be two tasks with same priority. <br />
 <em>queue</em> is a pointer to a message queue for the task. If the specified size is 0, no message queue is allocated for the task, and the queue parameter is ignored.<br />
 <em>Q_SIZE</em> is the length of the message queue in number of messages. Set to 0 if no queue should be used.</p>
<p><em>msgSize</em> is the size in bytes of the message type held in the message queue. Ignored of no queue is used.</p>
<p><b>Task Procedures</b><br />
When a task is created, a pointer to the task procedure is given as a parameter. Two tasks can reuse the same task procedure. In that case, one must be careful not to access wrong task data. All task specific data should be accessed through the data pointer passed to the <a class="el" href="os__task_8c.html#af531a501491c2acab61a65c8741e1375">task_create()</a> call. The task procedure can fetch the task data pointer by calling <a class="el" href="os__task_8c.html#a61293c64d8794582ff89ca7c27ba7d7d">task_get_data()</a>; The code section of a task procedure must begin and end with the task_open() and task_close() macros respectively. A very simple task is shown below: </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {</div>
<div class="line">    task_open();</div>
<div class="line">    ...</div>
<div class="line">    task_close();</div>
<div class="line">}</div>
</div><!-- fragment --><p> This task will execute only once. When the task_close() line is executed, the task is put in the killed state and will not be executed again. If the task has to synchronize with some external event, we can put a blocking call in the middle:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {</div>
<div class="line">    task_open();</div>
<div class="line">    ...</div>
<div class="line">    event_wait(evt);</div>
<div class="line">    ...</div>
<div class="line">    task_close();</div>
<div class="line">}</div>
</div><!-- fragment --><p> This task will run to the event_wait() call and then block. The kernel will schedule other tasks to execute and eventually the event will be signalled which will put the task back to ready state again. Execution will be resumed after the event_wait() call. Once the task_close() statement is reached, the task will be killed and will not be executed again.</p>
<p>If we want a task to be excuted periodically we must put the code into an endless loop: </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {</div>
<div class="line">    task_open();</div>
<div class="line">    <span class="keywordflow">for</span> (;;) {</div>
<div class="line">        ...</div>
<div class="line">        ...</div>
<div class="line">        task_wait(TASK_DELAY_TICKS);</div>
<div class="line">    }</div>
<div class="line">    task_close();</div>
<div class="line">}</div>
</div><!-- fragment --><p>Another example of a task waiting for characters to be received on the serial port: </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {</div>
<div class="line">    uint8_t data;</div>
<div class="line">    task_open();</div>
<div class="line">    <span class="keywordflow">for</span> (;;) {</div>
<div class="line">        event_wait(rxEvt);</div>
<div class="line">        uart_get(&amp;data);</div>
<div class="line">        ...</div>
<div class="line">    }</div>
<div class="line">    task_close();</div>
<div class="line">}</div>
</div><!-- fragment --><p> The rxEvt may be signalled from the uart ISR using event_ISR_signal(rxEvt).</p>
<p><b>Task API </b> <br />
The task API consists of the following functions and macros:</p><ul>
<li><a class="el" href="os__task_8c.html#af531a501491c2acab61a65c8741e1375">task_create()</a></li>
<li><a class="el" href="os__task_8c.html#ac98b20fb840196c24a41c72ae03cfe5f">task_kill()</a></li>
<li>task_open()</li>
<li>task_close()</li>
<li>task_suspend()</li>
<li>task_resume()</li>
<li><a class="el" href="os__task_8c.html#a250fb484aede328b860ec5661fec2ee4">task_state_get()</a></li>
<li>task_wait()</li>
<li><a class="el" href="os__task_8c.html#a61293c64d8794582ff89ca7c27ba7d7d">task_get_data()</a> </li>
</ul>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_bdd9a5d540de89e9fe90efdfc6973a4f.html">common</a></li><li class="navelem"><a class="el" href="dir_4e4d22e38ca4e2e8207825ee2d00bc54.html">src</a></li><li class="navelem"><a class="el" href="doc_tasks_8h.html">docTasks.h</a></li>
    <li class="footer">Generated on Tue Aug 28 2018 17:58:05 for cocoOS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.9.1 </li>
  </ul>
</div>
</body>
</html>
